package org.misty.practices.string;

import java.lang.reflect.Field;
import java.util.Arrays;

public class StringTestD {
    static final String JAVA = "Java";
    static final Field valueField;

    static {
        Field field;
        try {
            field = String.class.getDeclaredField("value");
            field.setAccessible(true);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
            field = null;
        }
        valueField = field;
    }

    public static void main(String[] args) throws IllegalAccessException {
        testD();
    }

    public static void testD() throws IllegalAccessException {
        var prefix = "a";
        var s1 = new String(prefix + "bc");
        var s2 = "abc";
        System.out.println(s1 == s2); // false

        byte[] bytes1 = (byte[]) valueField.get(s1);
        System.out.println(bytes1); // [B@ba4d54

        byte[] bytes2 = (byte[]) valueField.get(s2);
        System.out.println(bytes2); // [B@12bc6874

        byte[] bytes3 = (byte[]) valueField.get(s1.intern());
        System.out.println(bytes3); // [B@12bc6874

        byte[] bytes4 = (byte[]) valueField.get(s2.intern());
        System.out.println(bytes4); // [B@12bc6874
        System.out.println(s2 == s2.intern()); // true

        System.out.println(Arrays.equals(bytes1, bytes2)); // true
    }
}
